<?php
declare(strict_types=1);

namespace Service\DeclareService\PayDeclareCompany;

use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\Order;
use Service\VenderClient\PayCenterApi\WeixinApiClient;
use App\Model\BbcModel\OrderReportLog;
use App\Model\BbcModel\PayReport;
use App\Model\BbcModel\OrderReportStatus;
use Service\DeclareService\PayDeclareService;

class Wechatpay extends BaseService
{
    public function reportPay(array $body):array {
        $reportOrder=$body['reportOrder'];
        $payconfig=$body['payconfig'];
        $company=$body['company'];
        if(empty($reportOrder) || empty($payconfig) || empty($company)){
            return Normal::returnCode(Normal::SUC, '参数缺失,丢弃该条数据');
        }
        if(!empty($company['customs'])){
            $customs=trim($company['customs']);
        }else{
            $customs="GUANGZHOU_ZS";
        }
        $report_data=[
            'eCompanyCode'=>$company['eCompanyCode'],
            'transaction_id'=>$body['pay_serial'],
            'out_trade_no'=>(string)$body['pay_sn'],
            'customs'=>$customs,
        ];
        if(count($reportOrder)==1){
            $body['pay_sn'] = $reportOrder[0]['sn'];
            OrderReportLog::instance()->add([
                'order_id'=>$reportOrder[0]['id'],
                'sn'=>$body['pay_sn'],
                'content'=>"发起支付单申报",
                'cd'=>time()
            ]);
            $client = (new WeixinApiClient($payconfig));
            $result_call = $client->payDeclare($report_data);
            PayReport::instance()->add([
                'order_id'=>$reportOrder[0]['id'],
                'sn'=>$body['pay_sn'],
                'content'=>json_encode($result_call,JSON_UNESCAPED_UNICODE)
            ]);
            if($result_call['data']['return_code']=='SUCCESS'&&$result_call['data']['result_code']=='SUCCESS'){
                PayDeclareService::instance()->dealDeclareResult($body['pay_sn'], $reportOrder[0]['id'], '支付单申报成功');
                
                if($reportOrder[0]['need_order_report']==1){
                        //TODO订单申报
                    }
                
            }else{
                $errmsg=$result_call['data']['err_code_des']??'';
                PayDeclareService::instance()->dealDeclareResult($body['pay_sn'], $reportOrder[0]['id'], '支付单申报失败:请求'.$result_call['data']['return_msg'].$errmsg,false);
            }
        }else{
            $orderSNs= array_column($reportOrder, 'sn');
            $OrderReportStatusList=OrderReportStatus::instance()->lists([['sn','in',$orderSNs]]);
            $OrderReportStatusMapList= array_column($OrderReportStatusList, Null,'sn');
            $subOrderExtends=[
                'OrderReportStatusMapList'  =>$OrderReportStatusMapList,
                'pay_serial_extend'         =>''
            ];
            foreach ($reportOrder as $v){
                $report_data['sub_order_no']=$v['sn'];
                $report_data['order_fee']=round(bcsub((string)$v['price_order'],(string)$v['points_money'],2),2);
    //              $report_data['transport_fee']=$v['price_express'];
    //              $report_data['product_fee']=$v['price_order']-$v['price_express'];
                $report_data['transport_fee']=0;
                $report_data['product_fee']=$report_data['order_fee'];
                OrderReportLog::instance()->add(['order_id'=>$v['id'],'sn'=>$v['sn'],'content'=>"发起支付单申报",'cd'=>time()]);
                $client = (new WeixinApiClient($payconfig));
                $result_call = $client->payDeclare($report_data);
                PayReport::instance()->add(['order_id'=>$v['id'],'sn'=>$v['sn'],'content'=>json_encode($result_call,JSON_UNESCAPED_UNICODE)]);
                $report_res=$result_call['data'];
                if($report_res['return_code']=="SUCCESS" && $report_res['result_code']=='SUCCESS'){
                    $subOrderExtends['pay_serial_extend']=$report_res['sub_order_id'];
                    PayDeclareService::instance()->dealDeclareResult($v['sn'], $v['id'], '支付单申报成功',true,$subOrderExtends);
                    
                    if($v['need_order_report']==1){
                        //TODO订单申报
                    }
                    
                    
                }else{
                    $content="支付单申报失败:";
                    if(isset($result_call['msg'])){
                        $content.=$result_call['msg'];
                    }
                    $subOrderExtends['pay_serial_extend']='';
                    PayDeclareService::instance()->dealDeclareResult($v['sn'], $v['id'], $content,false,$subOrderExtends);
                }
            }
        }
        return Normal::returnCode(Normal::SUC, '操作成功');
    }
}
